2. +
Introduction
īŽ In this lecture weâre going to begin discussing how we actually
go about developing a complex program.
īŽ And weâll do it through the medium of object orientation.
īŽ This is likely to be a somewhat alien way to approach a
problem for many of you.
īŽ One of the great lies of object orientation is that it is âeasy because it
mirrors the way people thinkâ
īŽ However, it is the conceptual understanding that will enable you
to build very complex programs as time goes by.
īŽ Itâs a very powerful way to build functionality.
3. +
An Object Oriented System
īŽ A properly designed object oriented system consists of:
īŽ One or more objects.
īŽ These objects are defined by classes.
īŽ Each object consists of:
īŽ Attributes
īŽ Behaviours
īŽ Attributes contain information about the object.
īŽ Data
īŽ Behaviours are instructions for acting upon that data.
4. +
The Structure of an Object
īŽ First, we must define what we mean by an object.
īŽ In terms of object oriented programming.
īŽ An object is an instance of a class.
īŽ A class is like a blueprint telling the object what structure it has.
īŽ A class defines:
īŽ Attributes
īŽ Behaviours
īŽ The object defines
īŽ The state of attributes.
īŽ The values that each piece of data has.
5. +
Eh?
īŽ Letâs look at in the abstract.
īŽ There exists, somewhere, a blueprint for a chair.
īŽ Blueprints for the chairs on which you are sitting.
īŽ The blueprint defines what the chair looks like.
īŽ It defines the structure of the chair
īŽ It defines the relationship of the legs to the seat
īŽ This blueprint would be the class.
īŽ The specific chairs on which you are sitting would be objects.
īŽ They were made (instantiated) from that blueprint.
6. +
UhâĻ
īŽ The blueprint tells us how the chair is supposed to behave and
what information about that chair may be mutable.
īŽ The colour of the chair
īŽ The material of the chair
īŽ The size of the chair
īŽ The class says:
īŽ A chair has a colour, material, and size
īŽ The object says:
īŽ I am blue, made of leather, and am medium sized.
īŽ Different chairs can have the same basic structure, but different
states.
īŽ I am pink, made of suede, and am large.
7. +
Okay!
īŽ Weâll come back to this subject later.
īŽ Because regardless of what people may tell you, object orientation
does not come naturally to most people.
īŽ This however is going to be the fundamental bedrock of the systems
we analyse in the coming weeks.
īŽ The programming languages you use throughout this course
are object oriented languages.
īŽ Visual Basic .NET
īŽ Java
īŽ C#
īŽ The objects may not be emphasized to begin with.
īŽ But theyâre coming.
8. +
Attributes
īŽ The class defines what attributes will be present in an object.
īŽ It has these.
īŽ We donât know what they are yet.
īŽ Attributes are things that an object will have.
īŽ Usually things that are mutable (they can change).
īŽ Consider a human face.
īŽ It has eyes
īŽ It has a nose
īŽ It has a mouth
īŽ These are modeled in a computer program as variables.
īŽ A class is thus a collection of variables.
9. +
Behaviours
īŽ As well as these variables, a class contains behaviours for
acting upon those variables.
īŽ If the class is a human face:
īŽ Attributes: Eyes, Mouth, Nose
īŽ Behaviours: blink, smile, sniff
īŽ These are modeled in a computer program as functions.
īŽ Also called methods.
īŽ Two names for the same thing, weâll use these interchangeably.
īŽ Behaviours / Functions / Methods
īŽ Different words, all for the same basic concept.
10. +
Behaviours
īŽ Behaviours can be broken down into further parts.
īŽ Behaviours may have variables of their own.
īŽ Temporary variables that only exist as long as the method is
executing.
īŽ These are known as âlocal variablesâ in programming.
īŽ They have a limited scope within which they can be accessed.
īŽ Behaviours will usually incorporate programming structures.
īŽ Some structures allow the programmer to choose between
courses of actions.
īŽ Some structures allow the programmer to repeat blocks of code.
īŽ Behaviours will incorporate flow control.
11. +
Class Diagrams
īŽ One of the reasons why software development has such a
complex vocabulary of jargon is the need for experts to
communicate succinctly.
īŽ This is what all formal software design techniques are about.
īŽ In order for us to begin talking about how to discuss programs,
we need some kind of share vocabulary.
īŽ We donât know how to really use them yet, but letâs talk about
how we can represent a class in a modeling language.
īŽ In this case, the language is UML
īŽ Unified Modelling Language
12. +
Object Oriented Programs
īŽ Object oriented programs introduce new complexities of
interaction.
īŽ Objects have a relationship to classes.
īŽ Objects may be composed of other objects.
īŽ Objects define their own behaviours and attributes.
īŽ Objects can offer several levels of indirection to other objects.
īŽ Gaining a clear perspective on how an object oriented program
fits together is key to developing and maintaining OO code.
īŽ Which is what our analysis and design eventually leads towards.
13. +
Object Relationships
īŽ Objects can contain references to other objects.
īŽ This is known as composition.
īŽ This is a has a relationship.
īŽ Composition relationships also imply a multiplicity (or
cardinality).
īŽ Where there is a 0 or 1 relationship, it is known as a composition.
īŽ Where it is more, it is an aggregation.
īŽ Formal UML notations have more precise differentiations of
composition.
īŽ Thatâs for Future Us to worry about.
14. +
Object Relationships
īŽ An object may be a specialisation of another object.
īŽ This is known as inheritance in object orientation.
īŽ This is a is a relationship.
īŽ More specialised forms of inheritance exist.
īŽ Weâll talk about these as we go along.
īŽ For now, weâll just use the one kind.
īŽ The class from which another class inherits is called the parent.
īŽ Or Super class.
īŽ The class that does the inheriting is called the child.
īŽ Or sub class.
15. +
Examples
īŽ A Car:
īŽ Has an engine (composition)
īŽ Has four wheels (aggregation)
īŽ Has 2 or more doors (aggregation)
īŽ Is a vehicle (inheritance)
īŽ A Kitten:
īŽ Has four legs (aggregation)
īŽ Is an animal (inheritance)
īŽ Has a tail (composition)
īŽ Has the property of being as cute as the dickens (composition)
16. +
Object Oriented Programs
īŽ The relationship between objects and classes becomes very
complex in many computer programs.
īŽ Weâll see this before too long when we return to our case study.
īŽ Some means of representing this complexity âat a glanceâ is
useful.
īŽ This is what a class diagram is used for.
īŽ It gives the static view of a programâs architecture.
īŽ It doesnât change when the programming is running.
īŽ Other techniques exist too.
īŽ They vary in effectiveness.
17. +
Class Diagrams
īŽ At their simplest, class diagrams are just boxes with lines
connecting them to other boxes.
īŽ They show the relationship between classes only.
18. +
Class Diagrams
īŽ At the next level of usefulness, class diagrams also contain
information about attributes and methods.
īŽ Attributes in the second row
īŽ Methods in the third
īŽ At their best, class diagrams contain class relationships,
methods, attributes, and the visibility of each.
īŽ They also explicitly show multiplicity.
īŽ Class diagrams are the most fundamental unit of UML.
īŽ And also the most useful.
īŽ If you only ever take one diagraming notation to heart, let it be this
one.
20. +
Why Use A Class Diagram?
īŽ There are several reasons why using a class diagram leads to
a more complete view of a project.
īŽ It shows the relationship between classes in a way that allows you
to trace accessibility.
īŽ It shows at a glance the amount of coupling in a project.
īŽ It shows at a glance the amount of cohesion in a project.
īŽ It shows at a glance the impact of change.
īŽ These are elements that tell us âhow good an OO design we
haveâ
īŽ Thatâs an incredibly difficult thing to assess at the moment, which is
one of the reasons why OOP is so hard to learn.
21. +
Coupling and Cohesion
īŽ One of the biggest problems OO developers have is ensuring a
good OO design.
īŽ What is a good OO design, you ask?
īŽ Certainly for people unfamiliar with how OO is best done, itâs
hard to objectively rate an object architecture.
īŽ âUh, sure â itâs ten good?â
īŽ Two objective measures exist for this though.
īŽ Coupling
īŽ Cohesion
īŽ As a rule, we are aiming for high cohesion and low coupling.
22. +
Coupling
īŽ Coupling relates to the amount of interconnectedness of
classes.
īŽ We want this to be as low as is reasonably possible.
īŽ Thereâs a reason why we donât just link classes up in every possible
way.
īŽ A coupling relationship is any composition, aggregation, or
inheritance.
īŽ We canât get away from these, but we can limit them.
īŽ The more connections made between objects and classes, the
harder it is to make any changes.
īŽ Because of the knock-on effect associated.
23. +
Coupling
īŽ If your program has high coupling, there are several
disadvantages built in:
īŽ Changes in one object may result in unexpected side-effects
elsewhere in a program.
īŽ Often the cause is not at all obvious.
īŽ Classes are difficult to understand at a glance.
īŽ They make use of so much external functionality.
īŽ Classes are difficult to test.
īŽ They cannot be easily extracted from their context.
īŽ We aim for low coupling, of the right kind of coupling.
īŽ More on that later.
24. +
Cohesion
īŽ Cohesion is the level to which an object adheres to a single set
of responsibilities.
īŽ We want this as high as is reasonably possible.
īŽ In a good object oriented program, each object has one firmly
defined responsibility.
īŽ A cat object would not be responsible for keeping track of its
ownerâs employment.
īŽ A car object would not be responsible for storing the name of the
garage where it gets services.
īŽ Good OO design requires us to make classes that have unique,
specific responsibilities.
25. +
Cohesion
īŽ The dangers that come from low cohesion are the same as that
of high coupling.
īŽ Theyâre really just two ways of reflecting the same basic concept.
īŽ Low cohesion is an insidious problem.
īŽ Programs with low cohesion tend to become even less coherent
over time.
īŽ It is a consequence of badly analysed and badly designed
programs.
īŽ Youâll learn how to do this properly in third year.
26. +
The Tension of OO Development
īŽ Object Oriented programs are a tension between coupling and
cohesion.
īŽ We increase cohesion by increasing coupling.
īŽ We reduce coupling by decreasing cohesion.
īŽ As OO developers we have to find a happy medium between
the two.
īŽ In some cases, simple rationalization of a design can remove
problems.
īŽ That is a judgment call that comes from experience.
īŽ Weâll have plenty of opportunities to do this as we go through
the module.
27. +
Impact of Change
īŽ Both of these concepts are a way of objectively rating the
impact of change in a program.
īŽ What makes a program easy to work with?
īŽ Clearly written code.
īŽ Comments (seriously)
īŽ Changes do not have unexpected consequences.
īŽ The latter of these three is what the impact of change defines.
īŽ That when we change a piece of code, we know what is going to be
changed as a result of it.
28. +
Impact of Change
īŽ One of the reasons we restrict visibility of variables and
methods is to manage the impact of change.
īŽ If you are a Respectful Developer, you avoid making changes that
will impact on code you did not write.
īŽ Lest terrible vengeance be visited upon you and your kith and kin
īŽ The lower the visibility, the better.
īŽ Changing a local variable for example only impacts the method in
which it is defined.
īŽ Changing a class wide attribute will potentially impact on every
method in the class.
29. +
Impact of Change
īŽ In a multi-developer environment you have to assume that if
functionality is available, it has been used.
īŽ Someone saw your calculate_awesome() method and thought âhey,
that does exactly what I wantâ
īŽ If it can be used, you canât idly make structural changes.
īŽ Refactoring is fine
īŽ Redesign is not
īŽ Weâll talk more about refactoring in a later lecture.
īŽ It basically means âimproving code so well that nobody noticesâ
30. +
The Class Diagram
īŽ The best quality of class diagrams shows the impact of change
at a glance.
īŽ Where there are lots of arrows, there be coupling issues.
īŽ Where there are huge lists of methods and attributes in a class,
there be likely cohesion problems.
īŽ Where there are lots of public methods and attributes, there be likely
impact of change issues.
īŽ Doing the class diagram from the start gives us a good
opportunity to identify structural problems before we write a
single line of code.
31. +
The Class Diagram
īŽ In multi-developer projects, the class diagram represents the
âbest understandingâ of how a program is being developed.
īŽ Itâs kind of an informal contract with your fellow developers.
īŽ One of the benefits we get from object orientation is an ease of
development and integration.
īŽ But only if everyone is communicating effectively.
īŽ It is important that this diagram remain up to date with the state
of the program.
īŽ It is a âliving documentâ.
īŽ Some tools exist for automatically generating them.
īŽ And generating program code from class diagrams.
32. +
Conclusion
īŽ Class diagrams are awesome.
īŽ Seriously, you need to believe me on that.
īŽ They give us a neat way of representing a number of issues
with our programs.
īŽ How classes relate
īŽ The degree of coupling
īŽ The degree of cohesion
īŽ The likely impact of change.
īŽ We can tell at a glance when our systems are going off the
rails.
īŽ Huzzah.